package com.ulwx.tool.jms;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.apache.log4j.Logger;

import com.ulwx.tool.StringUtils;

public class JmsPoolFactory {


	private static final Logger log = Logger.getLogger(JmsPoolFactory.class);
	private static Map<String, PooledConnectionFactory> poollist = 
		new ConcurrentHashMap<String, PooledConnectionFactory>();

	private static JmsPoolFactory dbpoolFactory = new JmsPoolFactory();

	private JmsPoolFactory() {
		try{
			initPool();
		}catch(Exception e){
			log.error("",e);
		}
	}
	
	public static JmsPoolFactory getInstance() {
		return dbpoolFactory;
	}



	public static void setPoollist() throws Exception{

		try {
			poollist.clear();
			Map<String, Map<String, String>> maps = JmsReadConfig.getInstance().getProperties();

			Set<String> sets = maps.keySet();
			Iterator<String> iter = sets.iterator();
			while (iter.hasNext()) {
				try {

					String poolName = iter.next();
					Map<String, String> map = maps.get(poolName);

					
					String url = map.get("url");
					String user = StringUtils.trim(map.get("username"),ActiveMQConnection.DEFAULT_USER);
					String password = StringUtils.trim(map.get("password"),ActiveMQConnection.DEFAULT_PASSWORD);

					if(user.equals("DEFAULT_USER")){
						user=ActiveMQConnection.DEFAULT_USER;
					}
					if(password.equals("DEFAULT_PASSWORD")){
						password=ActiveMQConnection.DEFAULT_PASSWORD;
					}
					String idleTimeout = StringUtils.trim(map
							.get("idleTimeout"), "60");//60秒
					String maxConnections = StringUtils.trim(map
							.get("maxConnections"), "4");// 4
					String maximumActive = StringUtils.trim(map
							.get("maximumActive"), "200");// 每个连接200个session
					
					int int_idleTimeout=Integer.valueOf(idleTimeout)*1000;
					int int_maxConnections =Integer.valueOf(maxConnections);
					int int_maximumActive=Integer.valueOf(maximumActive);
				
					// String maxStatements =map.get("maxStatements");
					
					ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
							user,password, url);
					
					PooledConnectionFactory 
						pooledConnectionFactory=new PooledConnectionFactory(connectionFactory);
					pooledConnectionFactory.setIdleTimeout(int_idleTimeout);
					pooledConnectionFactory.setMaxConnections(int_maxConnections);
					pooledConnectionFactory.setMaximumActive(int_maximumActive);
					
					poollist.put(poolName, pooledConnectionFactory);
				} catch (Exception ex) {
					// TODO Auto-generated catch block
					throw ex;
				}
			}
		} catch (Exception e) {
			//log.error("", e);
			new Exception("add jms pool error!", e);
		}

	}


	synchronized public static void initPool() throws Exception{

		try {

			JmsPoolFactory.setPoollist();
			

		} catch (Exception e) {
			//log.error("", e);
			new Exception("add jms pool error!", e);
		}
	}

	synchronized  public  static void init(){
		dbpoolFactory = new JmsPoolFactory();
	}

	/**
	 * 根据key字符串获取连接池
	 * 
	 * @param key
	 * @return 连接池
	 * @throws Exception
	 */
	public static PooledConnectionFactory getJmsPool(String key) throws Exception {
		return getJmsPool(key, null);
	}

	
	public static PooledConnectionFactory getJmsPool() throws Exception {
		return getJmsPool("default", null);
	}
	/**
	 * 
	 * @param key
	 *            连接池名
	 * @param pros
	 *            返回的属性
	 * @return
	 * @throws Exception
	 */
	public static PooledConnectionFactory getJmsPool(String key, Map<String, String> pros)
			throws Exception {
		try {
			Map<String, Map<String, String>> maps = JmsReadConfig.getInstance().getProperties();
			if (pros != null) {
				pros.putAll(maps.get(key));
			}

			if (key == null)
				throw new Exception("JmsPool 'key' CANNOT be null");
			PooledConnectionFactory pf =  poollist.get(key);

			return pf;
		} catch (Exception e) {
			throw e;
		}
	}



	

	synchronized public static void clearAll() throws Exception {
		try {
			// 清空常规连接池
			Set<String> sets = poollist.keySet();
			Iterator<String> iter = sets.iterator();
			while (iter.hasNext()) {
				String poolName = iter.next();
				PooledConnectionFactory pf =poollist.get(poolName);
				pf.stop();

			}
			poollist.clear();
			
			

		} catch (Exception e) {
			throw new Exception("clear jms pool error!", e);
		}
	}



}
